package com.decent.ejfadmin.configure.controller;

import com.decent.ejfadmin.common.bean.MessageBean;
import com.decent.ejfadmin.common.bean.Page;
import com.decent.ejfadmin.common.bean.PageBean;
import com.decent.ejfadmin.config.OssConfig;
import com.decent.ejfadmin.configure.entity.UserPlatformBanner;
import com.decent.ejfadmin.configure.entity.dto.UserPlatformBannerDTO;
import com.decent.ejfadmin.configure.service.BannerConfigService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Encoder;

import java.io.IOException;
import java.util.Objects;

/**
 * describe:会员系统banner
 *
 * @author 陈豆豆
 * @date 2019/08/10
 */
@Slf4j
@RestController
@RequestMapping("bannerConfig")
public class BannerConfigController {
    @Autowired
    private BannerConfigService bannerConfigService;
    @Autowired
    private OssConfig ossConfig;

    /**
     * banner列表
     *
     * @param userPlatformBanner
     * @param page
     * @return
     */
    @RequestMapping("listBanner")
    public PageBean<UserPlatformBanner> listBanner(UserPlatformBanner userPlatformBanner, Page page) {
        return bannerConfigService.listBanner(userPlatformBanner, page);
    }

    /**
     * 新增Banner
     *
     * @param userPlatformBanner
     * @return
     */
    @RequestMapping(value = "addBanner", method = RequestMethod.POST)
    public MessageBean addBanner(UserPlatformBannerDTO userPlatformBanner) {
        try {
            if (Objects.isNull(userPlatformBanner)) {
                return MessageBean.fail("参数为空");
            }
            if (StringUtils.isBlank(userPlatformBanner.getPictureName())) {
                return MessageBean.fail("图片名称为空");
            }
            if (StringUtils.isBlank(userPlatformBanner.getImageString())) {
                return MessageBean.fail("请重新上传图片");
            }
            if (Objects.isNull(userPlatformBanner.getSort())) {
                return MessageBean.fail("排序为空");
            }
            return bannerConfigService.addBanner(userPlatformBanner);
        } catch (Exception e) {
            String errorMsg = "FileNotFoundException";
            if (e.getLocalizedMessage().contains(errorMsg)) {
                return MessageBean.fail("请选择图片");
            }
            log.warn("addBanner Error {} ", e, e);
            return MessageBean.fail(e.getLocalizedMessage());
        }
    }

    /**
     * 修改Banner
     *
     * @param userPlatformBanner
     * @return
     */
    @RequestMapping("updateBanner")
    public MessageBean updateBanner(UserPlatformBannerDTO userPlatformBanner) {
        try {
            if (Objects.isNull(userPlatformBanner) || Objects.isNull(userPlatformBanner.getId())) {
                return MessageBean.fail("参数为空");
            }
            if (StringUtils.isBlank(userPlatformBanner.getPictureName())) {
                return MessageBean.fail("图片名称为空");
            }
            if (Objects.isNull(userPlatformBanner.getSort())) {
                return MessageBean.fail("排序为空");
            }
            return bannerConfigService.updateBanner(userPlatformBanner);
        } catch (Exception e) {
            String duplicateEntry = "Duplicate entry";
            if (e.getLocalizedMessage().contains(duplicateEntry)) {
                return MessageBean.fail("banner排序已存在");
            }
            log.warn("修改Banner: updateBanner [{}]", e, e);
            return MessageBean.fail(e.getLocalizedMessage());
        }
    }

    /**
     * 删除Banner
     *
     * @param id
     * @return
     */
    @RequestMapping("deleteBanner")
    public MessageBean deleteBanner(@RequestParam Integer id) {
        try {
            return bannerConfigService.deleteBanner(id);
        } catch (Exception e) {
            log.warn("删除Banner:deleteBanner [{}]", e, e);
            return MessageBean.fail(e.getLocalizedMessage());
        }
    }

    /**
     * 转换base64
     *
     * @param imageStr
     * @return
     * @throws IOException
     */
    @RequestMapping("getByte")
    public MessageBean getByte(@RequestParam MultipartFile imageStr) throws IOException {
        MessageBean messageBean = new MessageBean();
        BASE64Encoder base64Encoder = new BASE64Encoder();
        String base64EncoderImg = "";
        if (imageStr != null) {
            base64EncoderImg = base64Encoder.encode(imageStr.getBytes());
        }
        messageBean.setSuccess(true);
        messageBean.setMessage(base64EncoderImg);
        return messageBean;
    }
}
